
import { login } from "@/api/auth.js";
import {
  setToken,
  removeToken
} from '@/utils/auth'

const state = {
};
const mutations = {
  SET_USERINFO: (state, info) => {
    state.userInfo = info
  },
};
const actions = {
  // 登录
  userLogin({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(res => {
        setToken(res.data.token)
        localStorage.setItem("userInfo", JSON.stringify(res.data.user));
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },


  // 前端 登出
  FedLogOut() {
    return new Promise(resolve => {
      localStorage.clear()
      removeToken()
      location.reload()// 为了重新实例化vue-router对象 避免bug
      resolve()
    })
  }
};

export default {
  state,
  mutations,
  actions,
};
